//
//  BudgetPieItemView.m
//  BaseProject
//
//  Created by mac on 2020/10/27.
//  Copyright © 2020 王洋. All rights reserved.
//

#import "BudgetPieItemView.h"

@interface BudgetPieItemView ()<UITextFieldDelegate>
@property(nonatomic,strong)UIImageView *leftIV;
@property(nonatomic,strong)UILabel *titleLab;
@property(nonatomic,strong)UIView *lineView;
@property(nonatomic,strong)UIButton *rightButton;
@property(nonatomic,strong)UILabel *moneyLab;
@property(nonatomic,strong)UITextField *textField;
@end

@implementation BudgetPieItemView
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        [self addTarget:self action:@selector(clickSelf:) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}
- (void)setIconModel:(BudgetPieItemModel *)iconModel
{
    _iconModel = iconModel;
    
    NSString *imagePath = [[NSBundle mainBundle]pathForResource:iconModel.imageName ofType:@"png"];
    UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
    self.leftIV.image = image;
    self.titleLab.text = iconModel.title;
    self.lineView.hidden = !iconModel.showLine;
    [self rightButton];
    self.moneyLab.text = @"¥";
    self.textField.text = [NSString stringWithFormat:@"%ld",iconModel.value];
}
#pragma mark - TF
- (void)addObserverPhoneTF:(UITextField *)tf
{
    self.iconModel.value = tf.text.integerValue;
    //[self realTimeCallBack];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    self.iconModel.value = textField.text.integerValue;
    textField.text = [NSString stringWithFormat:@"%ld",textField.text.integerValue];
    [self realTimeCallBack];
}
#pragma mark - click
- (void)clickSelf:sender
{
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
}
- (void)clickRightButton:sender
{
    [self.textField becomeFirstResponder];
}
#pragma mark - 回调
- (void)realTimeCallBack
{
    !self.realTimeCallBackBlock ?: self.realTimeCallBackBlock ();
}
#pragma mark - lazy懒加载
- (UIImageView *)leftIV
{
    if (!_leftIV) {
        UIImageView *iv = [[UIImageView alloc]init];
        [self addSubview:iv];
        iv.frame = CGRectMake(19*ADAPTER_WIDTH, 14*ADAPTER_WIDTH, 22*ADAPTER_WIDTH, 22*ADAPTER_WIDTH);
        _leftIV = iv;
    }
    return _leftIV;
}
- (UILabel *)titleLab
{
    if (!_titleLab) {
        UILabel *lab = [[UILabel alloc]init];
        [self addSubview:lab];
        lab.font = [UIFont systemFontOfSize:15*ADAPTER_WIDTH weight:UIFontWeightRegular];
        lab.textColor = RGB(51, 51, 51);
        lab.textAlignment = NSTextAlignmentLeft;
        lab.numberOfLines = 1;
        lab.frame = CGRectMake(54*ADAPTER_WIDTH, 0, self.width - 150*ADAPTER_WIDTH, self.height);
        _titleLab = lab;
    }
    return _titleLab;
}
- (UIView *)lineView
{
    if (!_lineView) {
        UIView *view = [[UIView alloc]init];
        view.backgroundColor = RGB(226, 226, 226);
        view.frame = CGRectMake(16*ADAPTER_WIDTH, self.height - 0.5, self.width - 32*ADAPTER_WIDTH, 0.5);
        [self addSubview:view];
        _lineView = view;
    }
    return _lineView;
}
- (UIButton *)rightButton
{
    if (!_rightButton) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:button];
        NSString *imagePath = [[NSBundle mainBundle]pathForResource:@"BudgetPie_edite" ofType:@"png"];
        UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
        [button setImage:image forState:UIControlStateNormal];
        button.frame = CGRectMake(self.width - 13*ADAPTER_WIDTH - 32*ADAPTER_WIDTH, 0, 13*ADAPTER_WIDTH + 32*ADAPTER_WIDTH, self.height);
        button.imageEdgeInsets = UIEdgeInsetsMake((self.height - 13*ADAPTER_WIDTH) * 0.5, 16*ADAPTER_WIDTH, (self.height - 13*ADAPTER_WIDTH) * 0.5, 16*ADAPTER_WIDTH);
        [button addTarget:self action:@selector(clickRightButton:) forControlEvents:UIControlEventTouchUpInside];
        _rightButton = button;
    }
    return _rightButton;
}
- (UITextField *)textField
{
    if (!_textField) {
        UITextField *tf = [[UITextField alloc]initWithFrame:CGRectMake(self.moneyLab.right + 10*ADAPTER_WIDTH, 0, self.width - self.moneyLab.right - 48*ADAPTER_WIDTH, self.height)];
        [self addSubview:tf];
        tf.textColor = RGB(250, 83, 76);
        tf.font = [UIFont systemFontOfSize:15*ADAPTER_WIDTH weight:UIFontWeightRegular];
        tf.clearButtonMode = UITextFieldViewModeWhileEditing;
        tf.keyboardType = UIKeyboardTypeNumberPad;//UIKeyboardTypeNumberPad UIKeyboardTypePhonePad电话板面 UIKeyboardTypeDecimalPad 小数点
        tf.tintColor = [UIColor orangeColor];
        tf.textAlignment = NSTextAlignmentLeft;
        [tf addTarget:self action:@selector(addObserverPhoneTF:) forControlEvents:UIControlEventEditingChanged];
        tf.secureTextEntry = NO;
        NSMutableAttributedString *placeHolder = [[NSMutableAttributedString alloc]initWithString:@"请输入金额～"];
        placeHolder.color = RGB(184, 184, 184);
        placeHolder.font = [UIFont systemFontOfSize:15*ADAPTER_WIDTH weight:UIFontWeightRegular];
        tf.attributedPlaceholder = placeHolder;
        tf.delegate = self;
        _textField = tf;
    }
    return _textField;
}
- (UILabel *)moneyLab
{
    if (!_moneyLab) {
        UILabel *lab = [[UILabel alloc]init];
        [self addSubview:lab];
        lab.font = [UIFont systemFontOfSize:15*ADAPTER_WIDTH weight:UIFontWeightRegular];
        lab.textColor = RGB(250, 83, 76);
        lab.textAlignment = NSTextAlignmentRight;
        lab.numberOfLines = 1;
        lab.frame = CGRectMake(228*ADAPTER_WIDTH, 0, 30*ADAPTER_WIDTH, self.height);
        _moneyLab = lab;
    }
    return _moneyLab;
}
@end
